﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Teigha.Core;

namespace Teigha.TD
{
	public class OdDbGraph : IDisposable
	{
		[EditorBrowsable(EditorBrowsableState.Never)]
		public OdDbGraph(IntPtr cPtr, bool cMemoryOwn)
		{
			this.swigCMemOwn = cMemoryOwn;
			this.swigCPtr = new HandleRef(this, cPtr);
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static HandleRef getCPtr(OdDbGraph obj)
		{
			if (obj != null)
			{
				return obj.swigCPtr;
			}
			return new HandleRef(null, IntPtr.Zero);
		}

		~OdDbGraph()
		{
			this.Dispose();
		}

		public virtual void Dispose()
		{
			if (this.swigCPtr.Handle != IntPtr.Zero)
			{
				if (this.swigCMemOwn)
				{
					lock (this.locker)
					{
						if (this.swigCPtr.Handle != IntPtr.Zero && this.swigCMemOwn)
						{
							this.swigCMemOwn = false;
							TD_DbPINVOKE.delete_OdDbGraph(this.swigCPtr);
						}
					}
					this.swigCMemOwn = false;
				}
				this.swigCPtr = new HandleRef(null, IntPtr.Zero);
			}
			GC.SuppressFinalize(this);
		}

		public OdDbGraph() : this(TD_DbPINVOKE.new_OdDbGraph(), true)
		{
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public OdDbGraphNode node(int nodeIndex)
		{
			OdDbGraphNode result = (OdDbGraphNode)Teigha.Core.Helpers.odrxCreateObjectInternal(TD_DbPINVOKE.OdDbGraph_node(this.swigCPtr, nodeIndex), false);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public OdDbGraphNode rootNode()
		{
			OdDbGraphNode result = (OdDbGraphNode)Teigha.Core.Helpers.odrxCreateObjectInternal(TD_DbPINVOKE.OdDbGraph_rootNode(this.swigCPtr), false);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public int numNodes()
		{
			int result = TD_DbPINVOKE.OdDbGraph_numNodes(this.swigCPtr);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public bool isEmpty()
		{
			bool result = TD_DbPINVOKE.OdDbGraph_isEmpty(this.swigCPtr);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public void addNode(OdDbGraphNode pNode)
		{
			TD_DbPINVOKE.OdDbGraph_addNode(this.swigCPtr, OdDbGraphNode.getCPtr(pNode));
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public void addEdge(OdDbGraphNode pFrom, OdDbGraphNode pTo)
		{
			TD_DbPINVOKE.OdDbGraph_addEdge(this.swigCPtr, OdDbGraphNode.getCPtr(pFrom), OdDbGraphNode.getCPtr(pTo));
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public void delNode(OdDbGraphNode pNode)
		{
			TD_DbPINVOKE.OdDbGraph_delNode(this.swigCPtr, OdDbGraphNode.getCPtr(pNode));
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public void reset()
		{
			TD_DbPINVOKE.OdDbGraph_reset(this.swigCPtr);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public void clearAll(byte flags)
		{
			TD_DbPINVOKE.OdDbGraph_clearAll(this.swigCPtr, flags);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public void getOutgoing(OdDbGraphNodeArray outgoing)
		{
			TD_DbPINVOKE.OdDbGraph_getOutgoing(this.swigCPtr, OdDbGraphNodeArray.getCPtr(outgoing));
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public virtual bool findCycles(OdDbGraphNode pStart)
		{
			bool result = TD_DbPINVOKE.OdDbGraph_findCycles__SWIG_0(this.swigCPtr, OdDbGraphNode.getCPtr(pStart));
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public virtual bool findCycles()
		{
			bool result = TD_DbPINVOKE.OdDbGraph_findCycles__SWIG_1(this.swigCPtr);
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public void breakCycleEdge(OdDbGraphNode pFrom, OdDbGraphNode pTo)
		{
			TD_DbPINVOKE.OdDbGraph_breakCycleEdge(this.swigCPtr, OdDbGraphNode.getCPtr(pFrom), OdDbGraphNode.getCPtr(pTo));
			if (TD_DbPINVOKE.SWIGPendingException.Pending)
			{
				throw TD_DbPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		private object locker = new object();

		private HandleRef swigCPtr;

		protected bool swigCMemOwn;
	}
}
